package com.fm.service;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

import static com.fm.service.service.TCPClient;

public class service_UpLoadFile {

    /**
     * 上传
     */
    public static void UpLoadFile(String num){

        //发送选项标识,建立连接
        Socket socket = TCPClient(num);

        try {
            // 从键盘获取文件路径
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入要上传的文件路径: ");
            String filePath = scanner.nextLine();

            // 构建文件对象并获取文件名
            File file = new File(filePath);
            String fileName = file.getName();
            System.out.println("文件名: "+fileName);

            //下载到本地
            byte[] bytes = new byte[1024];
            int len;
            if(!new File("src/main/resources/upLoad/" + fileName).exists()){
                FileInputStream fileInputStream = new FileInputStream(filePath);
                FileOutputStream fileOutputStream = new FileOutputStream("src/main/resources/upLoad/" + fileName);
                while ((len=fileInputStream.read(bytes))!=-1){
                    fileOutputStream.write(bytes,0,len);
                }
            }

            //发送名字
            OutputStream outputStream = socket.getOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            dataOutputStream.writeInt(fileName.getBytes().length);
            outputStream.write(fileName.getBytes());

            //写入数据
            FileInputStream fileInputStreamLocal = new FileInputStream("src/main/resources/upLoad/" + fileName);
            while ( (len = fileInputStreamLocal.read(bytes)) != -1 ){
                outputStream.write(bytes,0,len);
            }

            //不再写入
            socket.shutdownOutput();


            //接收返回值
            len = socket.getInputStream().read(bytes);
            System.out.println(new String(bytes,0,len));

            outputStream.close();

        }catch (Exception e) {
            e.printStackTrace();
        }












    }


    //    public static void UDPClient() {

    //        Scanner scanner = new Scanner(System.in);
    //
    //        // 从键盘获取文件路径
    //        System.out.println("请输入要上传的文件路径: ");
    //        String filePath = scanner.nextLine();
    //
    //        // 构建文件对象并获取文件名
    //        File file = new File(filePath);
    //        String fileName = file.getName();
    //        System.out.println("文件名: "+fileName);

    //        try {
    //            // 通过UDP发送文件名给服务端
    //            DatagramSocket socket = new DatagramSocket();
    //            InetAddress serverAddress = InetAddress.getByName("127.0.0.1");
    //
    //            byte[] fileNameBytes = fileName.getBytes();
    //            DatagramPacket fileNamePacket = new DatagramPacket(fileNameBytes, fileNameBytes.length, serverAddress, 12345);
    //            socket.send(fileNamePacket);
    //
    //            System.out.println("发送文件...");
    //            // 创建输入流
    //            FileInputStream fileInputStream = new FileInputStream(file);
    //            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
    //
    //            byte[] buffer = new byte[1024];
    //            int bytesRead;
    //
    //            // 从文件读取数据并发送到服务端
    //            while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
    //                DatagramPacket dataPacket = new DatagramPacket(buffer, bytesRead, InetAddress.getByName("127.0.0.1"), 12345);
    //                socket.send(dataPacket);
    //            }
    //
    //            //结束接收
    //            byte[] bytes = "quit".getBytes();
    //            socket.send(new DatagramPacket(bytes,0,bytes.length,InetAddress.getByName("127.0.0.1"),12345));
    //
    //            // 接收服务器返回的数据包
    //            byte[] receiveBuffer = new byte[1024];
    //            DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
    //            socket.receive(receivePacket);
    //
    //            // 处理接收到的数据
    //            String receivedMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
    //            System.out.println("Received from server: " + receivedMessage);
    //
    //
    //            // 关闭套接字和输入流
    //            socket.close();
    //            System.out.println("发送完成!");
    //            bufferedInputStream.close();
    //
    //
    //            // 关闭套接字
    //            socket.close();
    //        } catch (IOException e) {
    //            e.printStackTrace();
    //        }
    //    }



}
